<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="/common/meta.jsp"%>
<title>汪星客 - 个人中心 - 服务项目管理</title>
</head>
<body>
	<div class="wrap">
		<div class="header_bg">
			<%@ include file="/common/header.jsp"%>
	    </div>
	    <div class="w">
	    	<div class="path box">
	    		<jsp:include page="/common/frame/frame_path.jsp">
            		<jsp:param name="current" value="服务项目管理"/>
            	</jsp:include>
	        </div>
	    </div>
	    <div class="main">
	    	<div class="list box">
	        	<div class="pc_left">
            	<jsp:include page="/common/frame/frame_left.jsp">
            		<jsp:param name="checked" value="server"/>
            	</jsp:include>
            	</div>
	            <div class="pc_right">
                	<form id="frame_server_form">
	                <dl id="server_main_div" class="pc_m">
	                	<c:forEach items="${sysServerList}" var="server">
	                		<div class="form" valueType="${server.valueType}" serverCode="${server.serverCode}">
		                    	<p>
		                    		<label><input type="checkbox" value="${server.id}"/><span>${server.serverName}</span>
		                    			<c:if test="${server.valueType == 0}">（按每天计费）</c:if>
		                    			<c:if test="${server.valueType == 1}">（按每次计费）</c:if>
		                    		</label>
		                    	</p>
		                    	<c:forEach items="${server.petServerValuationList}" var="valuation">
		                    		<div valuation="${valuation.id}">
		                    		<c:choose>
										<c:when test="${valuation.valueName == null}">
											<input class="ipt_txt" type="text" name="valueVal" prompt="请填入价格"/>
										</c:when>
										<c:otherwise>
											<p class="width5 fl">${valuation.valueName}</p>
											<input class="ipt_txt2" type="text" name="valueVal" prompt="请填入价格"/>
										</c:otherwise>
									</c:choose>
									</div>
		                    	</c:forEach>
		                    	<c:forEach items="${server.petServerRestrainList}" var="restrain">
		                    		<div restrain="${restrain.id}">
		                    			<p class="width5 fl">${restrain.restrainName}</p>
		                    			<input class="ipt_txt2" type="text" name="restrainVal" prompt="请填入距离（km）"/>
		                    		</div>
		                    	</c:forEach>
		                    </div>
	                	</c:forEach>
	                    <div class="btn"><a href="javascript:void(0);" onclick="submitServerInfo(this)" class="easyui-linkbutton tg-btn">提交</a></div>
	                </dl>
	                </form>
	            </div>
	            <div class="clear"></div>
			</div>
	    </div>
	    <div class="footer_bg">
	    	<%@ include file="/common/footer.jsp"%>
	    </div>
	</div>
</body>

<script type="text/javascript">
function submitServerInfo() {
	var postData = {petServerList:[]};
	if(!$("#server_main_div>div :checkbox:checked").length) {
		msg.error("没有选服务");
		return;
	} 
	$("#server_main_div > div").each(function(){
		var $serverDiv = $(this);
		if($serverDiv.find(":checkbox").is(":checked")) {
			var petServer = {petServerValuationList:[], petServerRestrainList:[]};
			petServer.serverCode = $serverDiv.attr("serverCode");
			petServer.valueType = $serverDiv.attr("valueType");
			$serverDiv.children("div").each(function(){
				var $div = $(this);
				if($div.is("[restrain]")) {
					var restrain = {};
					restrain.sysId = $div.attr("restrain");
					restrain.restrainVal = $div.children("[name='restrainVal']").val();
					petServer.petServerRestrainList.push(restrain);
				} else {
					var valuation = {};
					valuation.sysId = $div.attr("valuation");
					valuation.valueVal = $div.children("[name='valueVal']").val();
					petServer.petServerValuationList.push(valuation);
				}
			});
			postData.petServerList.push(petServer);
		}
	});
	if($("#frame_server_form").form("validate")) {
		$.post("${ctx}/server/saveserver.do", tools.json2Data("host", postData), function(res){
			if(res.code == 1) {
				msg.msg("保存成功");
			} else {
				msg.error("保存出错");
			}
		}, "json");
	}	
}
/*
$.getJSON("${ctx}/server/getsys.do", function(serList) {
	var $mainDiv = $("#server_main_div");
	$.each(serList, function(index, server) {
		<p><label><input type="checkbox" /><span>寄养服务</span> （按每晚计费）</label></p>
		<input class="ipt_txt" type="text" value="请填入价格"/>
		
		<div class="form">
	    	<p><label><input type="checkbox" /><span>遛狗服务</span> （按每次计费）</label></p>
	        <div class="mar1"><p class="width5 fl">30分钟内</p><input class="ipt_txt2" type="text" value="请填入价格"/></div>
	        <div><p class="width5 fl">30-60分钟内</p><input class="ipt_txt2" type="text" value="请填入价格"/></div>
	        <div><p class="width5 fl">距离限制</p><input class="ipt_txt2" type="text" value="请填入距离（km）"/></div>
	    </div>
		
		var $serverDiv = $("<div></div>").addClass("form").attr({valueType:server.valueType, serverCode:server.serverCode});
		//$serverDiv.append("<input type='checkbox' value=" + server.id + "/>");
		$serverDiv.append($("<p><label><input type='checkbox' value=" + server.id + "/><span>" + server.serverName + "</span></label></p>").addClass("infoName"));
		if(server.petServerValuationList != null) {
			$.each(server.petServerValuationList, function(){
				var $valuationDiv = $("<div></div>").attr("valuation", this.id);
				if(this.valueName != null && this.valueName != "") {
					$valuationDiv.append("<span>" + this.valueName + "</span>");
				}
				$valuationDiv.append("<input name='valueVal'/>");
				$serverDiv.append($valuationDiv);
			});
		}
		if(server.petServerRestrainList != null) {
			$.each(server.petServerRestrainList, function(){
				var $restrainDiv = $("<div></div>").attr("restrain", this.id);
				$restrainDiv.append("<span>" + this.restrainName + "</span>");
				$restrainDiv.append("<input name='restrainVal'/>");
				$serverDiv.append($restrainDiv);
			});
		}
		$mainDiv.append($serverDiv);
	});
	serverEditInit();
});
*/

function serverEditInit() {
	$.getJSON("${ctx}/host/get.do", function(host){
		if($.isArray(host.petServerList)) {
			$.each(host.petServerList, function(index, petServer) {
				var $serverDiv = $("div[serverCode='" + this.serverCode + "']");
				if($serverDiv.length == 1) {
					$serverDiv.find(":checkbox").attr("checked", "checked");
					$.each(petServer.petServerValuationList, function() {
						$serverDiv.children("div[valuation='" + this.sysId + "']").children("input[name='valueVal']").css("color","#000000").val(this.valueVal);
					});
					if(petServer.petServerRestrainList != null) {
						$.each(petServer.petServerRestrainList, function() {
							$serverDiv.children("div[restrain='" + this.sysId + "']").children("input[name='restrainVal']").css("color","#000000").val(this.restrainVal);
						});
					}
				}
			});
		}
	});
}
$(function(){
	serverEditInit();
});
</script>
</html>